package com.heimo.gogocheque;

import java.text.NumberFormat;

import android.content.Context;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.heimo.gogocheque.core.ChineseDecimalFormat;
import com.heimo.gogocheque.core.EnglishDecimalFormat;

public class MainActivity extends BasicActivity {
	private EditText eText;
	private TextView numInput, txtChinese, labChinese, txtEnglish, labEnglish;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		fnForceKeyPad();
		fnUpdateFont();
		fnSetDecimalFormat();

	}

	private void fnUpdateFont() {
		eText = (EditText) findViewById(R.id.numpad);
		labChinese = (TextView) findViewById(R.id.labChinese);
		txtChinese = (TextView) findViewById(R.id.txtChinese);
		labEnglish = (TextView) findViewById(R.id.labEnglish);
		txtEnglish = (TextView) findViewById(R.id.txtEnglish);
		numInput = (TextView) findViewById(R.id.numInput);

		Typeface tf = Typeface.createFromAsset(getAssets(),
				"fonts/orbitron-bold.otf");
		
		eText.setTypeface(tf);
		labChinese.setTypeface(tf);
		txtChinese.setTypeface(tf);
		labEnglish.setTypeface(tf);
		txtEnglish.setTypeface(tf);
		numInput.setTypeface(tf);
	}

	private void fnForceKeyPad() {
		InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,
				InputMethodManager.HIDE_IMPLICIT_ONLY);
	}

	private void fnSetDecimalFormat() {

		eText.addTextChangedListener(new CurrencyTextWatcher(txtEnglish,txtChinese));
	}
}

class CurrencyTextWatcher implements TextWatcher {
	boolean mEditing;
	TextView txtEnglish,txtChinese;
	
	public CurrencyTextWatcher(TextView txtEnglish, TextView txtChinese) {
        mEditing = false;
        this.txtEnglish = txtEnglish;
        this.txtChinese = txtChinese;
    }
	
	public void afterTextChanged(Editable s) {
		if(!mEditing) {
            mEditing = true;
            String digits = fnGetParsedString(s.toString());
            s.replace(0, s.length(), digits);
            mEditing = false;
        }
	}

	public void beforeTextChanged(CharSequence s, int start, int count,
			int after) {
	}

	public void onTextChanged(CharSequence s, int start, int before,
			int count) {
		String val = fnGetParsedString(s.toString()); 
		txtChinese.setText(new ChineseDecimalFormat().convert(val));

		try {
			if (val.equalsIgnoreCase("")) {
				txtEnglish.setText("");
				return;
			}
			Integer valInt = Integer.parseInt(val);
			txtEnglish.setText(new EnglishDecimalFormat()
					.convert(valInt));
		} catch (Exception e) {
			txtEnglish.setText("Error : Number too big...");
		}
	}
	
	private String fnGetParsedString(String s){
		return s.replaceFirst("^0+(?!$)", "");
	}
}